package qmm.servlet.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import qmm.dao.UserDao;
import qmm.dao.impl.UserDaoImpl;
import qmm.entity.User;
import qmm.servlet.BaseServlet;

@WebServlet("/admin/AdminUserServlet")
public class AdminUserServlet extends BaseServlet{
	UserDao dao = new UserDaoImpl();
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		int page = Integer.parseInt(request.getParameter("page"));//ҳ
		int size = Integer.parseInt(request.getParameter("limit"));//ÿҳ
		String account = request.getParameter("account");
		String phone = request.getParameter("phone");
		
		List<User> list = dao.findAll(page,size,account,phone);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", dao.getCount(account,phone));
		map.put("data", list);
		String json = JSON.toJSONString(map);
		response.getWriter().write(json);
	}
	
	public void lock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int n = dao.lock(id);
		if(n>0){
			response.getWriter().print("success");
		}
	}
	
	public void resetPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int n = dao.resetPwd(id);
		if(n>0){
			response.getWriter().print("success");
		}
	}

}
